var express = require('express');
var router = express.Router();
const RtspStream = require('node-rtsp-stream');

let port = 9999;
const streams = {}
/* GET home page. */
router.post('/', function(req, res, next) {
  const responseData = req.body
  const postCurrent = port += 1
  const stream = new RtspStream({
    name: 'test-stream', // 流的名字，客户端可能会用到
    streamUrl: responseData.url, // 视频文件的路径，或视频源地址
    wsPort:postCurrent , // WebSocket端口，可选，用于调试
    ffmpegOptions: { // 传递给ffmpeg的额外参数
      '-preset': 'ultrafast',
    },
  });
  streams[postCurrent] = stream
  res.status(200).json({
    ws: `ws://localhost:${postCurrent}`,
    port:postCurrent
  });
 
});
router.get('/close', function(req, res, next) {
  if(req.query.port){
    streams[req.query.port].stop()
    delete streams[req.query.port]
    res.status(200).json({
      code:'success'
    });
  }else{
    res.status(200).json({
      code:'error'
    });
  }
});
module.exports = router;
